using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RetailAnalytic.Core;
using Microsoft.AnalysisServices.AdomdClient;

namespace RetailAnalytic.Tools.MDXTemplate
{
    public partial class UcVarType3 : UserControl
    {
        FrmFunctions _parF;
        bool vv = false;
        int sInd = 0;
        string hch = "";
        public List<string[]> lvl = new List<string[]>();
        public UcVarType3(FrmFunctions parForm)
        {
            InitializeComponent();
            _parF = parForm;
            uceX.DataSource = _parF.ls;
            uceX.SelectedIndex = 0;
            for (int i = 0; i <= _parF.dimensions.Count - 1; i++)
            {
                uceDim.Items.Add(_parF.dimensions[i]);
                uceDim.Items[i].Tag = _parF.dimensions[i];
            }
            uceDim.SelectedIndex = 0;
        }



        private void lv_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
            bool tr = true;
            if ((metadataTreeView1.SelectedNode.Level == 3) & (metadataTreeView1.SelectedNode.Index != 0))
            {
                if ((lv.Items.Count > 0) & (metadataTreeView1.SelectedNode.Parent.Parent.Text != hch))
                {
                    if (Dialog.Show(Dialog_type.Question, SR.StringLibrary.GetStrMSG(RetailAnalytic.SR.StringLibrary.KeyEnum.Ask_IS_CHANGE_CHOISE_HIERARHI_P1,
                        Environment.StackTrace, "", new string[] { metadataTreeView1.SelectedNode.Parent.Parent.Text }), new ButtonDialog[] { ButtonDialog.Button_yes, ButtonDialog.Button_no }) == ButtonDialog.Button_yes)
                    {
                        for (int i = 0; i <= lvl.Count - 1; i++)
                        {
                            string[] vt = lvl[i];
                            metadataTreeView1.Nodes[Convert.ToInt16(vt[3])].Nodes[Convert.ToInt16(vt[2])].Nodes[Convert.ToInt16(vt[1])].Nodes[Convert.ToInt16(vt[0])].ForeColor = Color.Gray;
                        }
                        lvl.Clear();
                        lv.Items.Clear();
                    }
                    else
                    {
                        tr = false;
                    }
                }
                if (tr)
                {
                    if (metadataTreeView1.SelectedNode.ForeColor == Color.Empty)
                    {
                        lv.Items.Add(e.Data.GetData(typeof(string)));
                        string[] it = { metadataTreeView1.SelectedNode.Index.ToString(), metadataTreeView1.SelectedNode.Parent.Index.ToString(), metadataTreeView1.SelectedNode.Parent.Parent.Index.ToString(), metadataTreeView1.SelectedNode.Parent.Parent.Parent.Index.ToString(), e.Data.GetData(typeof(string)).ToString() };
                        lvl.Add(it);
                        metadataTreeView1.SelectedNode.ForeColor = Color.Gray;
                    }
                    hch = metadataTreeView1.SelectedNode.Parent.Parent.Text;
                }

            }
            lv.Refresh();
        }

        private void lv_KeyDown(object sender, KeyEventArgs e)
        {
            int sel = 0;
            if ((e.KeyCode == Keys.Delete) & (lv.SelectedIndex > -1))
            {
                string[] vt = lvl[lv.SelectedIndex];
                metadataTreeView1.Nodes[Convert.ToInt16(vt[3])].Nodes[Convert.ToInt16(vt[2])].Nodes[Convert.ToInt16(vt[1])].Nodes[Convert.ToInt16(vt[0])].ForeColor = Color.Empty;
                lvl.RemoveAt(lv.SelectedIndex);
                sel = lv.SelectedIndex;
                lv.Items.RemoveAt(lv.SelectedIndex);
                if (sel == lv.Items.Count)
                { sel = 0; }
                if ((lv.Items.Count > 0) & (lv.SelectedIndices.Count == 0))
                {
                    lv.SelectedIndex = sel;
                }
            }
        }

        private void uceDim_ValueChanged(object sender, EventArgs e)
        {
            if ((lv.Items.Count > 0) & (!vv))
            {
                if (Dialog.Show(Dialog_type.Question, SR.StringLibrary.GetStrMSG(RetailAnalytic.SR.StringLibrary.KeyEnum.Ask_IS_SEL_OTHER_DIM_CLEAR_TUPLE, 
                    Environment.StackTrace, ""), new ButtonDialog[] { ButtonDialog.Button_yes, ButtonDialog.Button_no }) == ButtonDialog.Button_yes)
                {
                    lv.Items.Clear();
                    lvl.Clear();
                    vv = false;
                }
                else
                {
                    vv = true;
                    uceDim.SelectedIndex = sInd;
                }
            }
            metadataTreeView1.RetrieveMetadata(_parF.conn, _parF.dataMember, uceDim.Text, null);
            vv = false;
            sInd = uceDim.SelectedIndex;
        }


    }
}
